package for_Queue;

public class CSqQueueClass<T> {
    final int MaxSize = 5;
    private T[] data;
    private int front,rear;
    public CSqQueueClass(){
        data = (T[]) new Object[MaxSize];
        front=0;
        rear=0;
    }
    // 判断队列是否为空
    public boolean empty(){
        return rear==front;
    }
    // 进队
    public void push(T t){
        if((rear+1)%MaxSize==front){
            throw new IllegalArgumentException("队满");
        }
        rear = (rear+1)%MaxSize;
        data[rear] = t;
    }
    // 出队
    public T pop(){
        if(empty()){
            throw new IllegalArgumentException("队空");
        }
        front = (front+1)%MaxSize;
        return data[front];
    }
    // 取队头元素
    public T peek(){
        if(empty()){
            throw new IllegalArgumentException("队空");
        }
        return data[(front+1)%MaxSize];
    }
    // 返回队列元素
    public int len(){
        if(empty()){
            throw new IllegalArgumentException("队空");
        }
        if(rear>front){
            return rear-front;
        }
        else{
            return rear+MaxSize-front;
        }
    }

}
